package com.bd.rj.frontstage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bd.rj.common.entity.User;
import com.bd.rj.frontstage.service.IUserService;
import com.bd.rj.frontstage.service.UploadImageService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    @Qualifier("userServiceImpl")
    private IUserService iUserService;

    @Resource
    UploadImageService uploadImageService;
    /**
     * 小程序登录
     * @author 高瑾楠
     * @param username:用户名
     * @param password:密码
     * @return
     */
    @GetMapping("/login")
    public Map<String,Object> login(
            @RequestParam String username,
            @RequestParam String password
    ) throws NoSuchAlgorithmException {
        Map map = iUserService.login(username, password);
        map.put("code", 200);
        map.put("msg", "用户登录成功！");
        return map;
    }
    /**
     * 小程序注册用户
     * @author 高瑾楠
     * @param user
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseEntity<?> add(User user) {
        System.err.println("UserController -> add");
        Map<String, Object> map = new HashMap<>();
        // 检查用户名是否已存在
        boolean isUsernameExists = iUserService.isUsernameExists(user.getUsername());
        if (isUsernameExists) {
            map.put("code", 401);
            map.put("msg", "用户名已存在，请选择其他用户名！");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(map);
        }
        // 如果用户名不存在，继续注册逻辑
        String result = iUserService.register(user);
        map.put("code", 200);
        map.put("msg", "注册用户成功！");
        map.put("data", result);
        return ResponseEntity.ok(map);
    }

    /**
     * 个人中心息查询
     * @author 高瑾楠
     * @param username
     * @return
     */
    @GetMapping(value = "/getperson")
    public Map<String,Object> login(String username) {
        QueryWrapper <User> queryWrapper = new QueryWrapper<>();
        HashMap <String, Object> map = new HashMap<>();
        queryWrapper.eq("username", username);
        map.put("code", "200");
        map.put("msg", "查询成功！");
        map.put("data",iUserService.list(queryWrapper));
        return map;
    }
    /**
     * 个人中心信息修改
     * @author 高瑾楠
     * @param user
     * @return
     */
    @RequestMapping(value = "/updateuser", method = RequestMethod.POST)
    public Map<String, Object> update(User user) {
        System.err.println("UpdateUser -> update");
        Map<String, Object> map = new HashMap<>();
        boolean updateResult = iUserService.updateUser(user);
        if (updateResult) {
            map.put("code", 200);
            map.put("msg", "修改成功！");
            map.put("data", "更新成功");
        } else {
            map.put("code", 500);
            map.put("msg", "修改失败");
            map.put("data", "更新失败");
        }
        return map;
    }

    /**
     * @desc 上传用户头像
     * @author yxh
     */
    @RequestMapping(value = "uploaduserimg",method = RequestMethod.POST)
    public Map<String,Object> uploadUserImg(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id){
        System.out.println("用户id:"+id);
        Map<String,Object> map = new HashMap<>();
        if (!file.isEmpty()){
            String path = uploadImageService.uploadQNFile(file,"userimags/");
            if (path.equals("失败")){
                    map.put("code",201);
                    map.put("msg","上传失败");
                    map.put("data",file);
            }else {
                System.out.println("七牛云图片上传成功"+path);
                    map.put("code",200);
                    map.put("msg","上传成功");
                    map.put("data",path);
            }
            //更新用户头像
            User user =new User();
            user.setId(id);
            user.setUrl(path);
            iUserService.updateById(user);
        }
        return map;
    }
}